package com.sinosoft.emplmis.application.web;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.emplmis.application.web.dto.ResourceData;
import com.sinosoft.framework.core.api.domain.ResourceEntity;
import com.sinosoft.framework.core.api.service.ResourceService;

/**
 * 应用资源菜单信息控制器
 * @author xiaoxiong
 *
 */
@Controller
@RequestMapping("application/resource")
public class ResourceController {

	private Logger logger = LoggerFactory.getLogger(ResourceController.class.getName());
	
	@Autowired
	private ResourceService resourceService;
	
	/*public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}*/

	/**
	 * 默认页面
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "app/appMenu";
	}
	
	/**
	 * 根据角色编码获取资源信息
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/getResources",method = RequestMethod.GET)
	public @ResponseBody List<ResourceData> getResources(String roleId) {
		logger.info("调用服务：getResources开始");
		List<ResourceData> resourceDatas = new ArrayList<ResourceData>();
		List<ResourceEntity> resources = resourceService.getResourcesByRoleId(roleId);
		for(ResourceEntity resourceEntity : resources) {
			ResourceData resourceData = new ResourceData();
			resourceData.toResourceData(resourceData, resourceEntity);
			resourceDatas.add(resourceData);
		}
		logger.info("调用服务：getResources结束.");
		return resourceDatas;
	}
}
